from detector import DroneDetector
from camera_stream import CameraStream
from gui_controller import GUIController
from data_exporter import export_data
from config import CAMERA_SOURCE
from logger import get_logger
import cv2

logger = get_logger('Main')

def main():
    gui = GUIController()
    if not gui.run():
        logger.warning("GUI did not start detection.")
        return

    detector = DroneDetector()
    cam = CameraStream(source=CAMERA_SOURCE)

    logger.info("[SYSTEM] Starting real-time detection... Press 'q' to exit.")
    all_detections = []

    while True:
        frame = cam.read()
        if frame is None:
            break

        detections, annotated_frame = detector.detect_and_annotate(frame)
        all_detections.extend(detections)

        cv2.imshow('Drone Detection', annotated_frame)
        key = cv2.waitKey(1) & 0xFF
        if key == ord('q'):
            break

    cam.release()
    cv2.destroyAllWindows()
    if all_detections:
        export_data(all_detections)

if __name__ == '__main__':
    main()